﻿// 功能：Win32 API函数类
// 描述：
// 作者：温伟鹏
// 日期：2010/5/13 16:16:31

using System;
using System.Runtime.InteropServices;

namespace MeizuSDK.Presentation
{
    /// <summary>
    /// TimerProc委托
    /// </summary>
    /// <param name="param0"></param>
    /// <param name="param1"></param>
    /// <param name="param2"></param>
    public delegate void TimerProc(IntPtr param0, uint param1, uint param2);

    /// <summary>
    /// Win32 函数类
    /// </summary>
    public sealed class NativeMethods
    {
        /// <summary>
        /// 应用程序退出消息值
        /// </summary>
        public const uint WM_QUIT = 0x0012;

        /// <summary>
        /// 得到一个32bit数的低16bit 
        /// </summary>
        /// <param name="i"></param>
        /// <returns></returns>
        public static uint LOWORD(uint i)
        {
            return (i & 0x0000FFFF);
        }
        /// <summary>
        /// 得到一个32bit数的高16bit 
        /// </summary>
        /// <param name="i"></param>
        /// <returns></returns>
        public static uint HIWORD(uint i)
        {
            return ((i >> 16) & 0xffff);
        }

        #region 修改系统时间
        /// <summary>
        /// 系统时间结构
        /// </summary>
        [StructLayout(LayoutKind.Sequential)]
        internal struct SystemTime
        {
            public ushort wYear;
            public ushort wMonth;
            public ushort wDayOfWeek;
            public ushort wDay;
            public ushort wHour;
            public ushort wMinute;
            public ushort wSecond;
            public ushort wMiliseconds;

            /// <summary>
            /// 将DateTime转化为SysTime
            /// </summary>
            /// <param name="dt"></param>
            /// <returns></returns>
            internal static SystemTime FromDateTime(DateTime dt)
            {
                SystemTime sysTime = new SystemTime();

                sysTime.wYear = (ushort)dt.Year;
                sysTime.wMonth = (ushort)dt.Month;
                sysTime.wDay = (ushort)dt.Day;
                sysTime.wDayOfWeek = (ushort)dt.DayOfWeek;
                sysTime.wHour = (ushort)dt.Hour;
                sysTime.wMinute = (ushort)dt.Minute;
                sysTime.wSecond = (ushort)dt.Second;
                sysTime.wMiliseconds = (ushort)dt.Millisecond;

                return sysTime;
            }
        }
        /// <summary>
        /// 修改系统时间
        /// </summary>
        /// <param name="sysTime">系统时间</param>
        /// <returns></returns>
        [DllImport("coredll")]
        private static extern bool SetSystemTime(ref  SystemTime sysTime);
        /// <summary>
        /// 修改系统时间
        /// </summary>
        /// <param name="dt">系统时间</param>
        /// <returns></returns>
        public static bool SetSystemTime(DateTime dt)
        {
            SystemTime sysTime = SystemTime.FromDateTime(dt);

            return SetSystemTime(ref sysTime);
        }
        #endregion

        #region Safe Native Methods

        [DllImport("coredll", EntryPoint = "GetWindowThreadProcessId")]
        public static extern int GetWindowThreadProcessId(System.IntPtr hWnd, ref uint lpdwProcessId);

        [DllImport("coredll", CharSet = CharSet.Auto)]
        public static extern int GetCurrentThreadId();

        [DllImport("coredll", CharSet = CharSet.Auto)]
        public static extern uint SetTimer(IntPtr hwnd, uint idTimer, uint uTimeOut, TimerProc pfnTimerProc);

        [DllImport("coredll", CharSet = CharSet.Auto)]
        public static extern bool KillTimer(System.IntPtr hwnd, uint idEvent);

        /// <summary>
        /// 向应用程序发送退出消息
        /// </summary>
        /// <param name="nExitCode"></param>
        /// <returns></returns>
        [DllImport("Coredll.dll")]
        public static extern bool PostQuitMessage(int nExitCode);
        /// <summary>
        /// 向指定窗口发送消息
        /// </summary>
        /// <param name="hWnd"></param>
        /// <param name="msg"></param>
        /// <param name="wParam"></param>
        /// <param name="lParam"></param>
        /// <returns></returns>
        [DllImport("Coredll.dll")]
        public static extern bool PostMessage(IntPtr hWnd, uint msg, uint wParam, uint lParam);
        /// <summary>
        /// 将给定的窗口置为所有窗口最前方
        /// </summary>
        /// <param name="hWnd"></param>
        /// <returns></returns>
        [DllImport("coredll")]
        public static extern bool SetForegroundWindow(IntPtr hWnd);
        /// <summary>
        /// 根据指定名称查找窗口句柄
        /// </summary>
        /// <param name="lpClassName"></param>
        /// <param name="lpWindowName"></param>
        /// <returns></returns>
        [DllImport("coredll")]
        public static extern IntPtr FindWindowW(string lpClassName, string lpWindowName);
        /// <summary>
        /// 按照指定的方式显示给定的窗口
        /// </summary>
        /// <param name="hWnd"></param>
        /// <param name="nCmdShow"></param>
        /// <returns></returns>
        [DllImport("coredll")]
        private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
        /// <summary>
        /// 显示指定窗口
        /// </summary>
        /// <param name="hWnd">窗口句柄</param>
        /// <param name="hWndInsertAfter"></param>
        /// <param name="X"></param>
        /// <param name="Y"></param>
        /// <param name="cx">宽度</param>
        /// <param name="cy">高度</param>
        /// <param name="uFlags"></param>
        /// <returns></returns>
        [DllImport("coredll")]
        private static extern bool SetWindowPos(System.IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
        /// <summary>
        /// 设置父容器
        /// </summary>
        /// <param name="hWndChild"></param>
        /// <param name="hWndNewParent"></param>
        /// <returns></returns>
        [DllImport("coredll")]
        internal static extern System.IntPtr SetParent(System.IntPtr hWndChild, System.IntPtr hWndNewParent);
        /// <summary>
        /// 激活指定窗口
        /// </summary>
        /// <param name="hWnd"></param>
        /// <returns></returns>
        [DllImport("coredll")]
        internal static extern System.IntPtr SetActiveWindow(System.IntPtr hWnd);
        /// <summary>
        /// 发送Windows消息
        /// </summary>
        /// <param name="hWnd"></param>
        /// <param name="Msg"></param>
        /// <param name="wParam"></param>
        /// <param name="lParam"></param>
        /// <returns></returns>
        [DllImport("coredll")]
        internal static extern int SendMessage(IntPtr hWnd, uint Msg, uint wParam, uint lParam);
        /// <summary>
        /// 按照指定的方式显示给定的窗口
        /// </summary>
        /// <param name="hWnd"></param>
        /// <param name="showType"></param>
        /// <returns></returns>
        public static bool ShowWindow(IntPtr hWnd, NativeFormShowType showType)
        {
            int cmdShow = (int)showType;

            return ShowWindow(hWnd, cmdShow);
        }
        /// <summary>
        /// 显示窗口
        /// </summary>
        /// <param name="hWnd"></param>
        /// <param name="hWndInsertAfter"></param>
        /// <param name="location"></param>
        /// <param name="size"></param>
        /// <returns></returns>
        public static bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, System.Drawing.Point location, System.Drawing.Size size)
        {
            return SetWindowPos(hWnd, hWndInsertAfter, location.X, location.Y, size.Width, size.Height, 0);
        }


        #endregion

        #region UnSafe Native Methods
        [DllImport("coredll")]
        public extern static IntPtr GetDC(IntPtr hwnd);

        [DllImport("coredll.dll", SetLastError = true)]
        public static extern IntPtr CreateCompatibleDC(IntPtr hdc);

        [DllImport("coredll")]
        public extern static int ReleaseDC(IntPtr hwnd, IntPtr hdc);

        [DllImport("coredll", SetLastError = true)]
        public static extern int DeleteObject(IntPtr hdc);

        #endregion
    }
}
